﻿using UnityEngine;
using System;
using System.Collections.Generic;
using LuaInterface;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.U2D;

using BindType = ToLuaMenu.BindType;
using System.Reflection;

public static class CustomSettings
{
    public static string saveDir = Application.dataPath + "/LuaFramework/Source/Generate/";    
    public static string toluaBaseType = Application.dataPath + "/LuaFramework/ToLua/BaseType/";
    public static string baseLuaDir = Application.dataPath + "/LuaFramework/Tolua/Lua/";
    public static string injectionFilesPath = Application.dataPath + "/LuaFramework/ToLua/Injection/";

    //导出时强制做为静态类的类型(注意customTypeList 还要添加这个类型才能导出)
    //unity 有些类作为sealed class, 其实完全等价于静态类
    public static List<Type> staticClassTypes = new List<Type>
    {        
        typeof(UnityEngine.Application),
        typeof(UnityEngine.Time),
        typeof(UnityEngine.Screen),
        typeof(UnityEngine.SleepTimeout),
        typeof(UnityEngine.Input),
        typeof(UnityEngine.Resources),
        typeof(UnityEngine.Physics),
        typeof(UnityEngine.RenderSettings),
        typeof(UnityEngine.QualitySettings),
        typeof(UnityEngine.GL),
        typeof(UnityEngine.Graphics),
        //custom
		typeof(LuaInterface.Util),
		typeof(LuaInterface.LuaConst)
    };

    //附加导出委托类型(在导出委托时, customTypeList 中牵扯的委托类型都会导出， 无需写在这里)
    public static DelegateType[] customDelegateList = 
    {        
        _DT(typeof(Action)),                
        _DT(typeof(UnityEngine.Events.UnityAction)),
        _DT(typeof(System.Predicate<int>)),
		_DT(typeof(System.Action<int>)),
		_DT(typeof(System.Action<bool>)),
		// _DT(typeof(System.Action<string>)),
		// _DT(typeof(System.Action<bool,string>)),
		// _DT(typeof(System.Action<string,int,bool>)),
		_DT(typeof(System.Action<UnityEngine.Vector2>)),
		_DT(typeof(System.Comparison<int>)),
        _DT(typeof(System.Func<int, int>)),
		_DT(typeof(DG.Tweening.TweenCallback)),
		// _DT(typeof(Spine.AnimationState.TrackEntryDelegate)),
		// _DT(typeof(Spine.AnimationState.TrackEntryEventDelegate)),
		_DT(typeof(UnityEngine.Events.UnityAction<UnityEngine.Vector2>)),
		// _DT(typeof(UnityEngine.Events.UnityAction<bool>))
		// _DT(typeof(UnityEngine.Events.UnityAction<float>))
    };

    //在这里添加你要导出注册到lua的类型列表
    public static BindType[] customTypeList =
    {                
        //------------------------为例子导出--------------------------------
        //_GT(typeof(TestEventListener)),
        //_GT(typeof(TestProtol)),
        //_GT(typeof(TestAccount)),
        //_GT(typeof(Dictionary<int, TestAccount>)).SetLibName("AccountMap"),
        //_GT(typeof(KeyValuePair<int, TestAccount>)),
        //_GT(typeof(Dictionary<int, TestAccount>.KeyCollection)),
        //_GT(typeof(Dictionary<int, TestAccount>.ValueCollection)),
        //_GT(typeof(TestExport)),
        //_GT(typeof(TestExport.Space)),
        //-------------------------------------------------------------------        
                        
        _GT(typeof(LuaInjectionStation)),
        _GT(typeof(InjectType)),
        _GT(typeof(Debugger)).SetNameSpace(null),          

#if USING_DOTWEENING
        _GT(typeof(DG.Tweening.AutoPlay)),
        _GT(typeof(DG.Tweening.DOTween)),
        _GT(typeof(DG.Tweening.Tween)).SetBaseType(typeof(System.Object)).AddExtendType(typeof(DG.Tweening.TweenExtensions)),
        _GT(typeof(DG.Tweening.Sequence)).AddExtendType(typeof(DG.Tweening.TweenSettingsExtensions)),
        _GT(typeof(DG.Tweening.Tweener)).AddExtendType(typeof(DG.Tweening.TweenSettingsExtensions)),
		_GT(typeof(DG.Tweening.DOVirtual)),
		_GT(typeof(DG.Tweening.EaseFactory)),
		_GT(typeof(DG.Tweening.TweenParams)),
		_GT(typeof(DG.Tweening.DOTweenAnimation)),
		_GT(typeof(DG.Tweening.DOTweenPath)),
		_GT(typeof(DG.Tweening.DOTweenVisualManager)),
		_GT(typeof(DG.Tweening.Core.ABSSequentiable)),
		_GT(typeof(DG.Tweening.Core.TweenerCore<Vector3,DG.Tweening.Plugins.Core.PathCore.Path,DG.Tweening.Plugins.Options.PathOptions>)).AddExtendType(typeof(DG.Tweening.TweenSettingsExtensions)),
		_GT(typeof(DG.Tweening.Core.TweenerCore<Color, Color, DG.Tweening.Plugins.Options.ColorOptions>)),
		_GT(typeof(DG.Tweening.Core.TweenerCore<float,float,DG.Tweening.Plugins.Options.FloatOptions>)),
		_GT(typeof(DG.Tweening.Core.TweenerCore<string,string,DG.Tweening.Plugins.Options.StringOptions>)),
		_GT(typeof(DG.Tweening.Core.TweenerCore<Vector3, Vector3, DG.Tweening.Plugins.Options.VectorOptions>)).SetWrapName("TweenerCoreV3V3VO").SetLibName("TweenerCoreV3V3VO"),
		_GT(typeof(DG.Tweening.Core.TweenerCore<Vector2, Vector2, DG.Tweening.Plugins.Options.VectorOptions>)).SetWrapName("TweenerCoreV2V2VO").SetLibName("TweenerCoreV2V2VO"),
		_GT(typeof(DG.Tweening.Core.TweenerCore<UnityEngine.Quaternion,UnityEngine.Vector3,DG.Tweening.Plugins.Options.QuaternionOptions>)),

        _GT(typeof(Component)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Transform)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)).AddExtendType(typeof(TransformExtension)),
		_GT(typeof(Material)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Rigidbody)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		// _GT(typeof(Rigidbody2D)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions43)),
		// _GT(typeof(LineRenderer)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		// _GT(typeof(TrailRenderer)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),    
#else
                                         
        _GT(typeof(Component)),
        _GT(typeof(Transform)),
        _GT(typeof(Material)),
        _GT(typeof(Rigidbody)),
        //_GT(typeof(LineRenderer))
        //_GT(typeof(TrailRenderer))
#endif

      
        _GT(typeof(Camera)),
        _GT(typeof(Light)),
        _GT(typeof(AudioSource)),
        _GT(typeof(GameObject)),
        _GT(typeof(Application)),
        _GT(typeof(Physics)),
        _GT(typeof(Collider)),
        _GT(typeof(Time)),        
        _GT(typeof(Texture)),
        _GT(typeof(Texture2D)).AddExtendType(typeof(ImageConversion)),
        _GT(typeof(Shader)),        
        _GT(typeof(Renderer)),
        _GT(typeof(WWW)),
        _GT(typeof(Screen)),        
        _GT(typeof(AudioClip)),        
        _GT(typeof(AssetBundle)),
        _GT(typeof(ParticleSystem)),
        _GT(typeof(AsyncOperation)).SetBaseType(typeof(System.Object)),        
        _GT(typeof(SleepTimeout)),
        _GT(typeof(Animator)),
        _GT(typeof(Input)),
        _GT(typeof(SkinnedMeshRenderer)),
        _GT(typeof(MeshRenderer)).SetBaseType(typeof(Renderer)),
		_GT(typeof(UnityEngine.Events.UnityEvent)).SetBaseType(typeof(UnityEngine.Events.UnityEventBase)),

        //sprite
		_GT(typeof(Sprite)),
		_GT(typeof(SpriteRenderer)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions43)),
        _GT(typeof(SpriteAtlas)),


        //3d 物理
        _GT(typeof(BoxCollider)),
        _GT(typeof(MeshCollider)),
        _GT(typeof(SphereCollider)),        
        _GT(typeof(CharacterController)),
        _GT(typeof(CapsuleCollider)),
        

        //动画
        _GT(typeof(Animation)),        
        _GT(typeof(AnimationClip)).SetBaseType(typeof(UnityEngine.Object)),        
        _GT(typeof(AnimationState)),


        //ugui
		_GT(typeof(RectTransformUtility)),
		_GT(typeof(RectTransform)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)).AddExtendType(typeof(TransformExtension)),
		_GT(typeof(Text)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Image)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(RawImage)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(ScrollRect)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Button)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Toggle)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(CanvasGroup)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Slider)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(Graphics)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(InputField)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions46)).AddExtendType(typeof(DG.Tweening.ShortcutExtensions)),
		_GT(typeof(TextAnchor)),
        _GT(typeof(Canvas)),
		_GT(typeof(CanvasScaler)),
		_GT(typeof(UnityEngine.EventSystems.PointerEventData)),
		_GT(typeof(ScrollRect.ScrollRectEvent)).SetBaseType(typeof(UnityEngine.Events.UnityEvent<Vector2>)),
		_GT(typeof(Button.ButtonClickedEvent)).SetBaseType(typeof(UnityEngine.Events.UnityEvent)),
		// _GT(typeof(ToggleGroup)),
		// _GT(typeof(Toggle.ToggleEvent)).SetBaseType(typeof(UnityEngine.Events.UnityEvent<bool>)),
		// _GT(typeof(Slider.SliderEvent)).SetBaseType(typeof(UnityEngine.Events.UnityEvent<float>)),
		// _GT(typeof(VerticalLayoutGroup)),
		// _GT(typeof(HorizontalLayoutGroup)),
		// _GT(typeof(GridLayoutGroup)),
		// _GT(typeof(GridLayoutGroup.Corner)),
		// _GT(typeof(GridLayoutGroup.Axis)),
		// _GT(typeof(GridLayoutGroup.Constraint)),
		// _GT(typeof(ContentSizeFitter)),
		// _GT(typeof(ContentSizeFitter.FitMode)),
		// _GT(typeof(AspectRatioFitter)),
		// _GT(typeof(AspectRatioFitter.AspectMode)),


        //ugui 扩展
		// _GT(typeof(PageView)).SetBaseType(typeof(ScrollRect)),
		// _GT(typeof(PageIndicator)),
		// _GT(typeof(CenterView)).SetBaseType(typeof(ScrollRect)),
		// _GT(typeof(CenterViewItem)),
		// _GT(typeof(LoopScrollView)).SetBaseType(typeof(ScrollRect)),
		// _GT(typeof(UIPolygon)),
		// _GT(typeof(UGUIDrag)),
		// _GT(typeof(UGUIDrag.DragBackEffect)),
		// _GT(typeof(UGUIDrag.TriggerType)),
		// _GT(typeof(UIMask)),
		// _GT(typeof(ExtendImage)).SetBaseType(typeof(Image)),


        //全局设置
        _GT(typeof(QualitySettings)),
        _GT(typeof(RenderSettings)),                                                   
        _GT(typeof(BlendWeights)),           
        _GT(typeof(RenderTexture)),
        _GT(typeof(Resources)),     
        _GT(typeof(LuaProfiler)),



        //custom
		_GT(typeof(LuaInterface.Util)),
		_GT(typeof(LuaInterface.LuaComponent)),
		_GT(typeof(LuaInterface.Lua3DCollider)),
		_GT(typeof(LuaInterface.LuaManager)),
		_GT(typeof(LuaInterface.LuaConst)),
		_GT(typeof(LuaInterface.AssetsRef))
    };

    public static List<Type> dynamicList = new List<Type>()
    {
        typeof(MeshRenderer),
#if !UNITY_5_4_OR_NEWER
        typeof(ParticleEmitter),
        typeof(ParticleRenderer),
        typeof(ParticleAnimator),
#endif

        typeof(BoxCollider),
        typeof(MeshCollider),
        typeof(SphereCollider),
        typeof(CharacterController),
        typeof(CapsuleCollider),

        typeof(Animation),
        typeof(AnimationClip),
        typeof(AnimationState),

        typeof(BlendWeights),
        typeof(RenderTexture),
        typeof(Rigidbody),
    };

    //重载函数，相同参数个数，相同位置out参数匹配出问题时, 需要强制匹配解决
    //使用方法参见例子14
    public static List<Type> outList = new List<Type>()
    {
        
    };
        
    //ngui优化，下面的类没有派生类，可以作为sealed class
    public static List<Type> sealedList = new List<Type>()
    {
        /*typeof(Transform),
        typeof(UIRoot),
        typeof(UICamera),
        typeof(UIViewport),
        typeof(UIPanel),
        typeof(UILabel),
        typeof(UIAnchor),
        typeof(UIAtlas),
        typeof(UIFont),
        typeof(UITexture),
        typeof(UISprite),
        typeof(UIGrid),
        typeof(UITable),
        typeof(UIWrapGrid),
        typeof(UIInput),
        typeof(UIScrollView),
        typeof(UIEventListener),
        typeof(UIScrollBar),
        typeof(UICenterOnChild),
        typeof(UIScrollView),        
        typeof(UIButton),
        typeof(UITextList),
        typeof(UIPlayTween),
        typeof(UIDragScrollView),
        typeof(UISpriteAnimation),
        typeof(UIWrapContent),
        typeof(TweenWidth),
        typeof(TweenAlpha),
        typeof(TweenColor),
        typeof(TweenRotation),
        typeof(TweenPosition),
        typeof(TweenScale),
        typeof(TweenHeight),
        typeof(TypewriterEffect),
        typeof(UIToggle),
        typeof(Localization),*/
    };

    public static BindType _GT(Type t)
    {
        return new BindType(t);
    }

    public static DelegateType _DT(Type t)
    {
        return new DelegateType(t);
    }    


    [MenuItem("Lua/Attach Profiler", false, 151)]
    static void AttachProfiler()
    {
        if (!Application.isPlaying)
        {
            EditorUtility.DisplayDialog("警告", "请在运行时执行此功能", "确定");
            return;
        }

        LuaClient.Instance.AttachProfiler();
    }

    [MenuItem("Lua/Detach Profiler", false, 152)]
    static void DetachProfiler()
    {
        if (!Application.isPlaying)
        {            
            return;
        }

        LuaClient.Instance.DetachProfiler();
    }
}
